package com.yixun.controller;

import com.yixun.entity.YxProExistVariety;
import com.yixun.entity.YxSurveyXqRecord;
import com.yixun.entity.YxUser;
import com.yixun.service.YxProExistVarietyService;
import com.yixun.util.ResponseModel;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 畜禽品种管理(YxProExistVariety)表控制层
 *
 * @author lbx
 * @since 2021-06-16 15:42:41
 */
@Controller
@RequestMapping("yxProExistVariety")
public class YxProExistVarietyController extends BaseController{
    
    @GetMapping("index")
    public String goIndex(){
        return "yxProExistVariety/index";
    }
    @GetMapping("addPage")
    public String addAndUpdatePage(@Nullable Integer existVarietyId, Model model){
        if (existVarietyId!=null){
            System.out.println(existVarietyId);
            model.addAttribute("existVarietyId",existVarietyId);
        }
        return "yxProExistVariety/addPage";
    }
    
    
    
    /**
     * 服务对象
     */
    @Resource
    private YxProExistVarietyService yxProExistVarietyService;
    
    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @PostMapping("selectOne/{id}")
    @ResponseBody
    public Map<String, Object> selectOne(@PathVariable Long id) {
        return ResponseModel.getModel("success", "200", this.yxProExistVarietyService.queryById(id));
    }
    /**
     * 插入单条数据
     * @param yxProExistVariety 数据实体类封装
     * @return i
     */
    @PostMapping("insertOne")
    @ResponseBody
    public Map<String, Object> insertOne(@RequestBody YxProExistVariety yxProExistVariety){
        System.err.println(yxProExistVariety);
        YxUser yxUser = this.getSessionUser();
        yxProExistVariety.setAddUser(yxUser.getUser_id());
        int i = yxProExistVarietyService.insert(yxProExistVariety);
        return ResponseModel.getModel("success", "200", i);
    }
    
    /**
     * 分页查询
     * @param map start_num 当前页 page_size 每页条数
     * @return yxProCultureds 数据集合 list
     */
    @PostMapping("getList")
    @ResponseBody
    public Map<String, Object> selectListByPage(@RequestBody HashMap<String,Integer> map){
        List<YxProExistVariety> yxProExistVarieties = yxProExistVarietyService.queryAllByLimit(map.get("start_num"), map.get("page_size"));
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("items",yxProExistVarieties);
        hashMap.put("total",this.getCount());
        return ResponseModel.getModel("success", "200", hashMap);
    }
    @PostMapping("/getCount")
    @ResponseBody
    public Integer getCount(){
        return yxProExistVarietyService.getCount();
    }
    /**
     * 通过id更新单条数据  existVariety.getExistVarietyId()   not null
     * @param existVariety 封装好的实体
     * @return yxProExistVariety 更新后的数据
     */
    @PostMapping("updateOneById")
    @ResponseBody
    public Map<String, Object> updateOneById(@RequestBody YxProExistVariety existVariety){
        if (existVariety.getExistVarietyId() == null){
            return ResponseModel.getModel("failure，id不能为空", "200", null);
        }
        YxUser yxUser = this.getSessionUser();
        existVariety.setUpdateUser(yxUser.getUser_id());
        YxProExistVariety yxProExistVariety = yxProExistVarietyService.update(existVariety);
        return ResponseModel.getModel("success", "200", yxProExistVariety);
    }
    
    /**
     * 通过主键删除数据 将 is_delete 修改为 -1
     * @param id 主键
     * @return int 受影响数据数
     */
    @PostMapping("updateDelete/{id}")
    @ResponseBody
    public Map<String, Object> updateDelete(@PathVariable int id) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("existVarietyId",id);
        int i = this.yxProExistVarietyService.updateIsDelete(map);
        if (i>1){
            return ResponseModel.getModel("success", "200", i);
        }
        return ResponseModel.getModel("failure", "400", i);
    }
    @PostMapping("getNameById/{existVarietyId}")
    @ResponseBody
    public String getNameById(@PathVariable Integer existVarietyId){
        return this.yxProExistVarietyService.getNameById(existVarietyId);
    }
    @PostMapping("queryAll")
    @ResponseBody
    public List<YxProExistVariety> queryAll(){
        return this.yxProExistVarietyService.queryAll();
    }
    
    /**
     * @apiNote 条件搜索
     * @author 吕兵星
     * @date 2021/6/27 20:21
     * @param map 条件 字段 封装
     * @return java.util.List<com.yixun.entity.YxSurveyXqRecord>
     */
    @RequestMapping("getSearchList")
    @ResponseBody
    public HashMap<String, Object> getSearchList(@RequestBody HashMap<String,Object> map){
        System.err.println(map);
        ArrayList<String> culturedIds = null;
        ArrayList<String> varietyTypes = null;
        String varietyName = "";
        Integer offset = null;
        Integer limit = null;
        try {
            culturedIds = (ArrayList<String>) map.get("culturedId");
            varietyTypes = (ArrayList<String>) map.get("varietyType");
            varietyName = (String) map.get("varietyName");
            offset = (Integer) map.get("start_num");
            limit = (Integer) map.get("page_size");
        } catch (Exception e) {
            e.printStackTrace();
        }
        map.clear();
        if (culturedIds.size()>0) {
            map.put("culturedId", culturedIds.get(culturedIds.size()-1));
        }
        if (varietyTypes.size()>0) {
            map.put("varietyType", varietyTypes.get(varietyTypes.size()-1));
        }
        map.put("varietyName", varietyName);
        map.put("offset",offset);
        map.put("limit",limit);
        System.err.println(map);
        List<YxSurveyXqRecord> list = yxProExistVarietyService.searchAll(map);
        int total = yxProExistVarietyService.getSearchCount(map);
        map.put("items",list);
        map.put("total",total);
        return map;
    }
}
